/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamanpatokanhargabarangatkdangudang;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassSession;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.nilaipatokanharga.NilaiPatokanHarga;
import org.sadhar.sipp.nilaipatokanharga.NilaiPatokanHargaDAO;
import org.sadhar.sipp.nilaipatokanharga.NilaiPatokanHargaDAOImpl;
import org.sadhar.sipp.patokanharga.PatokanHarga;
import org.sadhar.sipp.patokanharga.PatokanHargaDAO;
import org.sadhar.sipp.patokanharga.PatokanHargaDAOImpl;
import org.sadhar.sipp.pembelianbarang.PembelianBarangDAO;
import org.sadhar.sipp.pembelianbarang.PembelianBarangDAOImpl;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

/**
 * 
 * @author Yohan Hardinugroho
 */
public class PerekamanPatokanHargaBarangAtkDanGudangWnd extends ClassApplicationModule {

    /**
     * 
     */
    private static final long serialVersionUID = -4265437438247919143L;
    private Listbox lstboxPatokanHarga;
    private Datebox dateboxTglPembelianMaks;
    private Label lblTahunAnggaran;
    private PembelianBarangDAO pembelianBarangDAO = new PembelianBarangDAOImpl();
    private PatokanHargaDAO patokanHargaDAO = new PatokanHargaDAOImpl();
    private NilaiPatokanHargaDAO nilaiPatokanHargaDAO = new NilaiPatokanHargaDAOImpl();
    private ClassSession session;
    private Session sesi;

    public void onCreate() throws Exception {
        sesi = Sessions.getCurrent();
        session = (ClassSession) sesi.getAttribute("CURRENT_SESSION");
        dateboxTglPembelianMaks = (Datebox) getFellow("dateboxTglPembelianMaks");
        lblTahunAnggaran = (Label) getFellow("lblTahunAnggaran");
        lstboxPatokanHarga = (Listbox) getFellow("lstboxPatokanHarga");

        // readyState();child
        // clear();
        load();
    }

    public void btnSimpanOnClick() throws Exception {

        String tahun = session.getThnAnggaran();
        Date tglBatasMaksimumPembelian = dateboxTglPembelianMaks.getValue();

        PatokanHarga phx = patokanHargaDAO.load(tahun);
        boolean phIsExist = patokanHargaDAO.isExist(tahun);
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
        int bandingTgl = 0;
        if (phIsExist) {
            bandingTgl = phx.getTglBatasMaximumPembelian().compareTo(tglBatasMaksimumPembelian);
        }
        if (bandingTgl == 0) {

            PatokanHarga ph = new PatokanHarga();
            ph.setTahun(tahun);
            ph.setTglBatasMaximumPembelian(tglBatasMaksimumPembelian);
            if (patokanHargaDAO.isExist(tahun)) {
                patokanHargaDAO.update(ph);
            } else {
                patokanHargaDAO.insert(ph);
            }

            nilaiPatokanHargaDAO.deleteByTahun(tahun);
            List<NilaiPatokanHarga> listNilaiPatokan = nilaiPatokanHargaDAO.listByTahunAndTanggalBeliMax(tahun,
                    tglBatasMaksimumPembelian);
            for (NilaiPatokanHarga nph : listNilaiPatokan) {
                nilaiPatokanHargaDAO.insert(nph);
            }
            // loadGridPatokanHarga();
            load();

        } else {
            if (Messagebox.show(
                    " Apakah akan mengganti patokan harga dari tanggal " + sdf.format(phx.getTglBatasMaximumPembelian()) + " menjadi tanggal " + sdf.format(tglBatasMaksimumPembelian) + "?",
                    "Pesan Konfirmasi", Messagebox.YES | Messagebox.NO, null) == Messagebox.YES) {

                PatokanHarga ph = new PatokanHarga();
                ph.setTahun(tahun);
                ph.setTglBatasMaximumPembelian(tglBatasMaksimumPembelian);
                if (patokanHargaDAO.isExist(tahun)) {
                    patokanHargaDAO.update(ph);
                } else {
                    patokanHargaDAO.insert(ph);
                }

                nilaiPatokanHargaDAO.deleteByTahun(tahun);
                List<NilaiPatokanHarga> listNilaiPatokan = nilaiPatokanHargaDAO.listByTahunAndTanggalBeliMax(tahun,
                        tglBatasMaksimumPembelian);
                for (NilaiPatokanHarga nph : listNilaiPatokan) {
                    nilaiPatokanHargaDAO.insert(nph);
                }
                // loadGridPatokanHarga();
                load();
            }
        }
    }/////////////else

    public void btnBatalOnClick() throws Exception {
        // loadGridPatokanHarga();
        load();

    }

    public void loadGridPatokanHarga() {
        lstboxPatokanHarga.getItems().clear();
        List<Map> listMap = patokanHargaDAO.select();

        for (final Map m : listMap) {
            Listitem item = new Listitem();
            item.setValue(m);
            item.appendChild(new Listcell(ClassAntiNull.AntiNullString(m.get("tahun"))));
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            item.appendChild(new Listcell(sdf.format(ClassAntiNull.AntiNullDate(m.get("tglBatasMaksimumPembelian")))));

            Toolbarbutton daftarHargaToolbar = new Toolbarbutton();
            daftarHargaToolbar.setLabel("[daftar harga]");
            daftarHargaToolbar.addEventListener("onClick", new EventListener() {

                @Override
                public void onEvent(Event arg0) throws Exception {
                    final Window win = (Window) Executions.createComponents(
                            "sippzul/perekamanpatokanhargabarangatkdangudang/DaftarHargaPopup.zul",
                            getRoot(), null);
                    Textbox txtboxKeySearch = (Textbox) win.getFellow("txtboxKeySearch");
                    txtboxKeySearch.setValue(ClassAntiNull.AntiNullString(m.get("tahun")));
                    //
                    win.doModal();
                }
            });

            final Toolbarbutton toolbarHapus = new Toolbarbutton();
            toolbarHapus.setLabel("[hapus]");
            toolbarHapus.addEventListener("onClick", new EventListener() {

                @Override
                public void onEvent(Event arg0) throws Exception {
                    // /////////////////
                    if (Messagebox.show("Apakah Data Tahun " + m.get("tahun").toString() + " Akan Dihapus: ",
                            "Pesan Konfirmasi", Messagebox.YES | Messagebox.NO, null) == Messagebox.YES) {
                        try {
                            patokanHargaDAO.deleteByTahun(ClassAntiNull.AntiNullString(m.get("tahun")));

                        } catch (Exception ex) {
                            Messagebox.show("Gagal menghapus data: " + ex.getMessage());
                        }
                        Window window = (Window) getRoot().getFellow(
                                "contentDiv").getFirstChild();
                        Button btnBatal = (Button) window.getFellow("btnBatal");
                        toolbarHapus.addForward(null, btnBatal, "onClick");
                    }

                }
            });
            Hbox box = new Hbox();
            box.appendChild(daftarHargaToolbar);
            box.appendChild(toolbarHapus);
            Listcell lbox = new Listcell();
            lbox.appendChild(box);
            item.appendChild(lbox);
            lstboxPatokanHarga.appendChild(item);

        }
    }

    public void dateBoxOnChange() {
        Date fromDateBox = dateboxTglPembelianMaks.getValue();
        if (fromDateBox.after(new Date())) {
            try {
                Messagebox.show(
                        "Tidak Diperbolehkan Memasukkan Tanggal Melebihi Tanggal Hari Ini",
                        "Pesan Kesalahan", Messagebox.OK, null);
                dateboxTglPembelianMaks.setValue(new Date());
                dateboxTglPembelianMaks.setFocus(true);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    private void load() {
        try {
            // lblTahunAnggaran.setValue(ClassSession.getInstance().getTahunAkademik());
            lblTahunAnggaran.setValue(session.getThnAnggaran());
            Date tglNotaTerbaru = pembelianBarangDAO.listTglNotaTerbaru();
            dateboxTglPembelianMaks.setValue(tglNotaTerbaru);
            loadGridPatokanHarga();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
